'''
Created on 2009-11-7

@author: jazy
pcap types for python
'''

import ctypes




class sockaddr(ctypes.Structure):
    '''
    struct sockaddr {
    unsigned  short  sa_family;     /* address family, AF_xxx */
    char  sa_data[14];                 /* 14 bytes of protocol address */
    };
    '''
    _fields_=[("sa_family",ctypes.c_uint16),("sa_data",ctypes.c_char*14)]
    
    
class pcap_addr(ctypes.Structure):
    '''
    struct pcap_addr {
    struct pcap_addr *next;
    struct sockaddr *addr;        /* address */
    struct sockaddr *netmask;    /* netmask for that address */
    struct sockaddr *broadaddr;    /* broadcast address for that address */
    struct sockaddr *dstaddr;    /* P2P destination address for that address */
    };
    '''
    pass

pcap_addr._fields_=[("next",ctypes.POINTER(sockaddr)),\
                    ("addr",ctypes.POINTER(sockaddr)),\
                    ("netmask",ctypes.POINTER(sockaddr)),\
                    ("broadaddr",ctypes.POINTER(sockaddr)),\
                    ("dstaddr",ctypes.POINTER(sockaddr))]

class pcap_if(ctypes.Structure):
    '''
    struct pcap_if {
    struct pcap_if *next;
    char *name;        /* name to hand to "pcap_open_live()" */
    char *description;    /* textual description of interface, or NULL */
    struct pcap_addr *addresses;
    bpf_u_int32 flags;    /* PCAP_IF_ interface flags */
    };
    '''
    pass

pcap_if._fields_=[("next", ctypes.POINTER(pcap_if)), ("name", ctypes.c_char_p), \
                   ("description", ctypes.c_char_p),("addresses",ctypes.POINTER(pcap_addr)),\
                    ("flags",ctypes.c_uint32)]
                 



class pcap_file_header(ctypes.Structure):
    '''
    struct pcap_file_header {
    bpf_u_int32 magic;
    u_short version_major;
    u_short version_minor;
    bpf_int32 thiszone;    /* gmt to local correction */
    bpf_u_int32 sigfigs;    /* accuracy of timestamps */
    bpf_u_int32 snaplen;    /* max length saved portion of each pkt */
    bpf_u_int32 linktype;    /* data link type (LINKTYPE_*) */
    };
    '''
    pass

class pcap_pkthdr(ctypes.Structure):
    '''
    struct pcap_pkthdr {
    struct timeval ts;    /* time stamp */
    bpf_u_int32 caplen;    /* length of portion present */
    bpf_u_int32 len;    /* length this packet (off wire) */
    };
    '''
    _fields_ = [("tv_sec", ctypes.c_long), ("tv_usec", ctypes.c_long), ("caplen", ctypes.c_uint), ("len", ctypes.c_uint)]

class pcap_stat(ctypes.Structure):
    '''
    struct pcap_stat {
    u_int ps_recv;        /* number of packets received */
    u_int ps_drop;        /* number of packets dropped */
    u_int ps_ifdrop;      /* drops by interface XXX not yet supported */
    #ifdef WIN32
    u_int bs_capt;        /* number of packets that reach the application */
    #endif /* WIN32 */
    };
    '''
    _fields_ = [("ps_recv", ctypes.c_uint), ("ps_drop", ctypes.c_uint), ("ps_ifdrop", ctypes.c_uint)]



